<?php
class Image {
	public function thumb($img, $w, $h) {
		//创建画布
		$dseImg = imagecreatetruecolor($w, $h);
		//原图
//		获取图片类型
		$tist = ltrim(strrchr($img, '.'), '.');
		switch (true) {
			case $tist == 'jpg'||$tist == 'jpeg' :
				$srcImg = imagecreatefromjpeg($img);
				break;
			case $tist == 'gif' :
				$srcImg = imagecreatefromgif($img);
				break;
			case $tist == 'png' :
				$srcImg = imagecreatefrompng($img);
				break;
			default :
				break;
		}
		$srcW = imagesx($srcImg);
		$srcH = imagesy($srcImg);
		//拷贝图像
		imagecopyresized($dseImg, $srcImg, 0, 0, 0, 0, $w, $h, $srcW, $srcH);

		//保存图片
		switch (true) {
			case $tist == 'jpg'||$tist == 'jpeg' :
				imagejpeg($dseImg, './shanghai_ws.jpg');
				break;
			case $tist == 'gif' :
				imagegif($dseImg, './shanghai_ws.gif');
				break;
			case $tist == 'png' :
				imagepng($dseImg, './shanghai_ws.png');
				break;
			default :
				break;
		}
		//销毁图片
		imagedestroy($dseImg);
		imagedestroy($srcImg);
	}

	public function water($img, $logo) {
		$tist = ltrim(strrchr($img, '.'), '.');
		$tist1 = ltrim(strrchr($logo, '.'), '.');
		switch (true) {
			case $tist == 'jpg'||$tist == 'jpeg' :
				$dstImg = imagecreatefromjpeg($img);
				break;
			case $tist == 'gif' :
				$dstImg = imagecreatefromgif($img);
				break;
			case $tist == 'png' :
				$dstImg = imagecreatefrompng($img);
				break;
			default :
				break;
		}
		$dstH = imagesy($dstImg);
		$dstW = imagesx($dstImg);
		switch (true) {
			case $tist1 == 'jpg'||$tist1 == 'jpeg' :
				$srcImg = imagecreatefromjpeg($logo);
				break;
			case $tist1 == 'gif' :
				$srcImg = imagecreatefromgif($logo);
				break;
			case $tist1 == 'png' :
				$srcImg = imagecreatefrompng($logo);
				break;
			default :
				break;
		}
		$srcH = imagesy($srcImg);
		$srcW = imagesx($srcImg);
		$dstY = $dstH - $srcH - 10;
		$dstX = $dstW - $srcW - 10;
		imagecopymerge($dstImg, $srcImg, $dstX, $dstY, 0, 0, $srcW, $srcH, 88);
		switch (true) {
			case $tist == 'jpg'||$tist == 'jpeg' :
				imagejpeg($dstImg, './shanghai_w.jpg');
				break;
			case $tist == 'gif' :
				imagegif($dstImg, './shanghai_w.gif');
				break;
			case $tist == 'png' :
				imagepng($dstImg, './shanghai_w.png');
				break;
			default :
				break;
		}
		imagedestroy($dstImg);
		imagedestroy($srcImg);
	}

}

$i = new Image();
//缩略动作
$i -> thumb('./shanghai.jpg', 800, 600);
//水印动作
$i -> water('./shanghai.jpg', './logo.png');
